//
//  AppDelegate.swift
//  新浪微博
//
//  Created by yzl on 15/7/27.
//  Copyright © 2015年 yjc. All rights reserved.
//

import UIKit
 let JCRootViewControllerSwitchNotification = "JCRootViewControllerSwitchNotification"
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        print(userAccount.shareAccount)
         /// 注册通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchViewController:", name: JCRootViewControllerSwitchNotification, object: nil)
        setupApperance()

        // 创建window并设置大小
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        // 设置tabBar控制器为根控制器
        window?.rootViewController = defaultViewController()
        // 显示
        window?.makeKeyAndVisible()
        
    
        return true
    }
    /// 注销通知
    //deinit {
//        NSNotificationCenter.defaultCenter().removeObserver(self)
//    }
   // 切换控制器
    func switchViewController(n:NSNotification) {
        print("切换\(n)")
        let mainVc = n.object as! Bool
        window?.rootViewController = mainVc ? MainViewController() : WelcomeViewController()
    }
    
private func defaultViewController() -> UIViewController {
        // 判断用户是否登录，没有登录返回主控制器
    if !userAccount.userLogin {
        return MainViewController()
    }
    // 判断是否有新版本，如果有，返回新特性，如果没有返回欢迎界面
    return ischeckNewBook() ? NewFeatureViewController() : WelcomeViewController()
}
    
    
/// 检查新版本
    func ischeckNewBook()->Bool {
    // 1,获取当前版本
let currentVersion = Double(NSBundle .mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)!
    //2,获取以前版本
        let sandBoxVersion = "sandBoxVersion"
    let oldVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandBoxVersion)
    //3，把当前版本保存到偏好设置
    NSUserDefaults.standardUserDefaults().setDouble(currentVersion, forKey: sandBoxVersion)
        // 4,返回新版本
        return currentVersion > oldVersion
        
    }
/// 统一设置外观
    private func setupApperance() {
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
       // UITabBar.appearance().tintColor = UIColor.orangeColor()
    }
   
}

